var map = { extra: "" };
(function(){
	map.objs = {
		//arc: { beep: { array: [0,1,2], bool: true, boo: false, single: 0, too: "hi" } },
		arc: {},
		base: {},
		drawInfo: {},
		meshbox: {},
		cone: {},
		define: {},
		dynamicColor: {},
		group: {},
		link: {},
		material: {},
		mesh: {},
		physics: {},
		meshpyra: {},
		sphere: {},
		teleporter: {},
		textureMatrix: {},
		weapon: {},
		zone: {}
	};

	map.makeobj = function() {
		var templ = {
			meshbox: { namespace: "phy", name: "name", position: ["pos"], size: ["size"], rotation: "rot", phydrv: "phydrv", matref: "matref",
				bool: { shootthru: 0, drivethru: 0, noradar: 0 }
			}
		};
		templ.meshpyr = templ.base = templ.teleporter = templ.meshbox;
		templ.base.team = "team";
		templ.teleporter.border = "border"; delete templ.teleporter.matref;
		
		var objcount = 0;
		for (k in map.objs[tobj]) { objcount++; }
		
		$.each(templ[tobj], function(k, v){
			var ns = templ[tobj].namespace, pid = "editor-" + ns + "-" + v;
			if (k == "namespace") { return true; }
			if (v.constructor == String) { templ[tobj][k] = id(pid).value;
			} else if (v.constructor == Array && k != "boo") { $("*[id^=" + pid + "]").each(function(i, de){ templ[tobj][k][i] = de.checked || de.value; });
			} else if (k == "bool") { $.each(v, function(cb) { templ[tobj][k][cb] = id("editor-" + ns + "-" + cb).checked; });
			}
		});
		
		if (templ[tobj].name === "" || templ[tobj].name === null || !templ[tobj].name) { templ[tobj].name = tobj + objcount; }
		
		objs[tobj][ templ[tobj].name ] = templ[tobj];
	};

	map.export = {
		json: function(){
			return xpcom.i.json().encode(map.objs);
		},
		
		bzw: function(){
			var bzw = "## Made with jBZW version " + jbzw.version + " ##\n## jbzw.bzflag.bz ##\n\n" + map.extra, line;
			
			$.each(map.objs, function(type, list){
				bzw += "## " + type + " ##\n";
				
				$.each(list, function(k, obj){
					bzw += type + "\n\tname " + k + "\n";
					
					$.each(obj, function(param, value){
						line = false;
					
						if (value == true) {
							line = param;
						} else if (value.constructor == Array) {
							line = param + " " + value.join(" ");
						} else if (typeof value == "string" || typeof value == "number") {
							line = param + " " + value;
						}
						bzw += line ? "\t" + line + "\n" : "";
					});
					bzw += "end\n";
				});
			});
			return bzw;
		}
	};
	
	map.import = {
		json: function(str){
			return xpcom.i.json().decode(str);
		},
		
		bzw: function(str){
			var lines = str.split(/^/mg), line, obj;
			
			for (var i = 0; i <= lines.length; i++) {
				console.log(lines[i]);
				obj = {}; line = $.trim(lines[i].replace(/#.*/, "")); // clear obj; remove any comments and trim
				if (line.length == 0) { continue; } // it's empty or was a comment
				
				if (map.objs[line] && typeof map.objs[line] == "object") { // it's an object, parse it
					obj.type = line; i++;
					for (; i <= lines.length; i++) {
						line = $.trim(lines[i].replace(/#.*/, "")); // remove any comments and trim
						if (line.length == 0) { continue; } else if (line == "end") { break; } // it's empty or was a comment, or is the end of the obj
						
						line = line.match(/^\s*([^\s]*)\s?(.*)?/);
						console.log(line);
						/*switch (line[1]) {
							case "position": case "size": case "spin": // array
								obj.obj[line[1]] = line[2].replace(/\s\s/g, " ").split(" ");
								break;
							case "drivethrough": case "shootthrough": // bool
								obj.obj[line[1]] = true;
								break;
							//case "phydrv" // string
						}*/
					}
					
					map.objs[obj.type] = obj.obj;
				} else { // it isn't a reconized object, add it to "extra"
					map.extra += line + "\n";
				}
			}
		}
	};
})();